<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

    protected $_facebook, $_breadcrumb, $_data, $_loggedUser = NULL, $_loginStatus = "", $_relatedContent = Array(), $_keywords = Array();
    private $_fetchedUserData;
    protected $_controllerTime;

    public function __construct($fetchedUserData = Array()) {
        $this->_controllerTime = microtime(true);
        parent::__construct();
        $this->load->library("FacebookAPI", $this->config->item("fb-api"));
        $this->_facebook = $this->facebookapi;
        $this->_breadcrumb = array();
        $this->_data = array();
        $this->_data["menuHL"] = "Home";
        $this->_data["pageTitle"] = "";
        $this->_data["useMap"] = false;
        $this->_data["useEditor"] = false;
        $this->_data["useSlideShow"] = false;
        $this->_data["useDocViewer"] = false;
        $this->_fetchedUserData = $fetchedUserData;
        $this->_updateLoginSession();
        $this->_data["loggedUser"] = $this->_loggedUser;
        $this->_data["loginStatus"] = $this->_loginStatus;
        $this->_data["relatedContent"] = $this->_relatedContent;
        $this->_data["imageFB"] = res_url("img/logo_ps.png");
        $this->_data["titleFB"] = "";
        $this->_data["isArticle"] = false;
        $this->_data["descFB"] = "Perdesaan Sehat (PS) merupakan realisasi atas kebijakan Percepatan Pembangunan Kualitas Kesehatan Berbasis Perdesaan di Daerah Tertinggal. PS dikembangkan untuk menjadi bagian dari program Percepatan Pembangunan Daerah Tertinggal (PPDT) dari Kementerian Pembangunan Daerah Tertinggal (KPDT). Kebijakan PS dicanangkan secara resmi oleh KPDT bersama Solidaritas Istri Kabinet Indonesia Bersatu (SIKIB) pada kegiatan Desa Sejahtera yang bertempat di Entikong, Kalimantan Barat tanggal 20 Desember 2012.";
        $this->_keywords = Array(
            "perdesaan", "pedesaan",
            "sehat", "kesehatan",
            "kpdt", "kementrian", "daerah", "tertinggal", "indonesia",
            "hanibal", "hamidi",
            "dokter", "puskesmas",
            "bidan", "desa",
            "sanitasi",
            "air", "bersih",
            "gizi"
        );
    }

    protected function _updateLoginSession() {
        // update & check user data
        $this->load->library('session');
        $useremail = $this->session->userdata('useremail');
        $userpwd = $this->session->userdata('userpwd');
        $loggedUser = NULL;
        if (!$useremail) {
            // ==> no user email
            $this->_loginStatus = "NOSESSION";
        } else {
            if (!$userpwd) {
                // ==> no password
                $this->_loginStatus = "NOSESSION";
            } else {
                // check db
                $this->load->model('User_model');
                $userBasic = $this->User_model->fetchLogin($useremail, $userpwd);
                if ($userBasic != NULL) {
                    $this->_loginStatus = "LOGIN";
                    $loggedUser = $userBasic;
                    // fetch details if requested
                    if (in_array("details", $this->_fetchedUserData)) {
                        $userBasic->detail = $this->User_model->fetchDetail($userBasic->id);
                    }
                    if (in_array("groups", $this->_fetchedUserData)) {
                        $userBasic->groups = $this->User_model->fetchGroups($userBasic->id);
                    }
                    if (true) {//key_exists("roles", $this->_fetchedUserData) && $this->_fetchedUserData["roles"]) {
                        $userBasic->roles = $this->User_model->fetchRoles($userBasic->id);
                    }
                } else {
                    // ==> not found
                    $this->_loginStatus = "NOTFOUND";
                }
            }
        }
        $this->_loggedUser = $loggedUser;
    }

    protected function _renderPage($view, $authorizedRoles = Array()) {
        $authorized = true;
        if (count($authorizedRoles) > 0) {
            $authorized = false;
            if ($this->_loggedUser != NULL) {
                foreach ($authorizedRoles as $req) {
                    if (in_array($req, $this->_loggedUser->roles)) {
                        $authorized = true;
                        break;
                    }
                }
            }
        }
        if ($authorized) {
            $data = $this->_data;
            $baseData = array();
            $baseData["facebook"] = $this->_facebook;
            $baseData["view"] = $view;
            $baseData["crumbs"] = $this->_breadcrumb;
            $baseData["content"] = $this->load->view($view, array_merge($data, $baseData), true);
            $baseData["breadcrumb"] = $this->load->view("breadcrumb", array_merge($data, $baseData), true);
            $keywordString = "";
            foreach ($this->_keywords as $keyword) {
                if ($keywordString != "") {
                    $keywordString .= ",";
                }
                $keywordString .= $keyword;
            }
            $baseData["keywords"] = $keywordString;
            $baseData["elapsedTime"] = (microtime(true) - $this->_controllerTime);
            $this->load->view("base", $baseData);
        } else {
            redirect("home/error");
        }
    }

    protected final function addBreadCrumb($text, $url) {
        array_push($this->_breadcrumb, array("text" => $text, "url" => $url));
    }

    protected final function addKeyword($text) {
        if (!in_array($text, $this->_keywords)) {
            array_push($this->_keywords, $text);
        }
    }

    protected final function isAuth($authorizedRoles) {
        $authorized = false;
        if ($this->_loggedUser != NULL) {
            foreach ($authorizedRoles as $req) {
                if (in_array($req, $this->_loggedUser->roles)) {
                    $authorized = true;
                    break;
                }
            }
        }
        return $authorized;
    }

}

?>
